package org.uim.cricket.analyzer.parsers.bowling.impl;

import java.util.HashMap;

import org.uim.cricket.analyzer.parsers.bowling.BowlingInningParsable;
import org.uim.cricket.analyzer.parsers.misc.URLIDParsable;
import org.uim.cricket.analyzer.parsers.misc.URLTitleParsable;
import org.uim.cricket.entities.Player;
import org.uim.cricket.entities.bowling.BowlerStatistics;
import org.uim.cricket.entities.bowling.BowlingInning;
import org.uim.cricket.util.HTMLStringUtils;
import org.uim.cricket.util.MyStringUtils;

public class DefaultBowlingInningParser implements BowlingInningParsable {

    private URLIDParsable urlIdParser = null;
    private URLTitleParsable urlTitleParser = null;
    
    public DefaultBowlingInningParser() {
        
    }
    
    public DefaultBowlingInningParser(URLIDParsable theUrlIDParser, URLTitleParsable theUrlTitleParser) {
        this.urlIdParser = theUrlIDParser;
        this.urlTitleParser = theUrlTitleParser;
    }
    
	@Override
	public BowlingInning parseBowlingInning(StringBuilder sb) {
	    if (sb == null || sb.length() == 0) return null;
        BowlingInning bowl = new BowlingInning();
        
        String source = HTMLStringUtils.cleanHTMLComments(sb).toString();
        String[] rows = MyStringUtils.splitInBetween(source, "<td", "</td>");
        
        if (rows.length < 8) return null;
        
        this.setPlayerNameRow(rows[1], bowl);
        
        BowlerStatistics stats = new BowlerStatistics();
        
        stats.setOvers(HTMLStringUtils.cleanHTMLTags("<td" + rows[2]));
        stats.setMaidnes(Integer.parseInt(HTMLStringUtils.cleanHTMLTags("<td" + rows[3])));
        stats.setRuns(Integer.parseInt(HTMLStringUtils.cleanHTMLTags("<td" + rows[4])));
        stats.setWickets(Integer.parseInt(HTMLStringUtils.cleanHTMLTags("<td" + rows[5])));
        
        this.setBowlingExtras(HTMLStringUtils.cleanHTMLTags("<td" + rows[7]), stats);
        
        bowl.setStatistics(stats);
        
        return bowl;
	}
	
	private void setPlayerNameRow(String row, BowlingInning bowl) {
        int pos = row.indexOf('>');
        row = row.substring(pos + 1);
        
        pos = row.indexOf('<');
        int epos = row.indexOf('>', pos + 1);
        String playerURLTag = row.substring(pos, epos + 1);
        
        HashMap<String, String> attribs = HTMLStringUtils.getHTMLAttributes(playerURLTag);
        Player player = new Player();
        if (this.urlIdParser != null) {
            player.setId(this.urlIdParser.parseUrlID(attribs.get("href")));
        } else {
            player.setId(attribs.get("href"));
        }
        if (this.urlTitleParser != null) {
            player.setShortName(this.urlTitleParser.parseUrlTitle(attribs.get("title")));
        } else {
            player.setShortName(attribs.get("title"));
        }
        
        String cleanedName = HTMLStringUtils.cleanHTMLTags(row);
        if (cleanedName.indexOf('*') >= 0) {
            player.setCaptain(true);
            cleanedName = cleanedName.replace("*", "");
        }
        if (cleanedName.indexOf("&dagger;") >= 0) {
            player.setWicketKeeper(true);
            cleanedName = cleanedName.replace("&dagger;", "");
        }
        cleanedName = cleanedName.trim();
        player.setTitleName(cleanedName);
        
        bowl.setBowler(player);
    }
	
	private void setBowlingExtras(String row, BowlerStatistics bowlstat) {
	   if (row == null || row.trim().length() == 0) return;
	   
	   int pos = row.indexOf('(');
	   int epos = row.lastIndexOf(')');
	   row = row.substring(pos + 1, epos);
	   String[] exs = row.split("[,]");
	   
	   for (int i=0;i<exs.length;i++) {
	       if (exs[i].indexOf("nb") >= 0) {
	           exs[i] = exs[i].replace("nb", "").trim();
	           bowlstat.setNoballs(Integer.parseInt(exs[i]));
	       } else if (exs[i].indexOf("w") >= 0) {
	           exs[i] = exs[i].replace("w", "").trim();
               bowlstat.setWides(Integer.parseInt(exs[i]));
	       }
	   }
	   
	}
	
	public void setUrlTitleParser(URLTitleParsable urlTitleParser) {
        this.urlTitleParser = urlTitleParser;
    }
	
	public void setUrlIdParser(URLIDParsable urlIdParser) {
        this.urlIdParser = urlIdParser;
    }
	
	public URLTitleParsable getUrlTitleParser() {
        return urlTitleParser;
    }
	
	public URLIDParsable getUrlIdParser() {
        return urlIdParser;
    }
	
}
